{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 800x600 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import geopandas as gpd\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import textwrap\n",
    "import helper_400\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "helper_400.set_sns_style()\n",
    "\n",
    "import plotting\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "hs_dict = plotting.get_hotspopt_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = pd.read_parquet(\"outputs/d2-events-2d-230929.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "hspts = plotting.get_hotspot_list()\n",
    "df = df[['GEOID']+hspts]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = df.groupby(\"GEOID\").sum().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df.to_parquet(\"outputs/agg_hotspots-231003.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# data = gdf.merge(df, left_on=\"FIPS\", right_on=\"GEOID\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# data.to_file(\"outputs/d2-events-2d-230929_shapefile.geojson\", driver='GeoJSON')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add Vunerability Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# cols = [\n",
    "#     \"STCNTY\",\n",
    "#     \"FIPS\",\n",
    "#     \"E_AFAM\",\n",
    "#     \"EP_AFAM\",\n",
    "#     \"E_TOTPOP\",\n",
    "#     \"EP_POV150\",\n",
    "#     \"EP_UNEMP\",\n",
    "#     \"EP_AGE65\",\n",
    "#     \"RPL_THEMES\",\n",
    "# ]\n",
    "# vind = pd.read_csv(\"data/California.csv\", usecols=cols, dtype={\"FIPS\": str})\n",
    "# vind = vind[vind[\"RPL_THEMES\"] > -1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "file_names = [\n",
    "\"SVI2020_ARIZONA_tract.gdb\",\n",
    "\"SVI2020_CALIFORNIA_tract.gdb\",\n",
    "\"SVI2020_COLORADO_tract.gdb\",\n",
    "\"SVI2020_IDAHO_tract.gdb\",\n",
    "\"SVI2020_MONTANA_tract.gdb\",\n",
    "\"SVI2020_NEVADA_tract.gdb\",\n",
    "\"SVI2020_NEWMEXICO_tract.gdb\",\n",
    "\"SVI2020_OREGON_tract.gdb\",\n",
    "#\"SVI2020_TEXAS_tract.gdb\",\n",
    "\"SVI2020_UTAH_tract.gdb\",\n",
    "\"SVI2020_WASHINGTON_tract.gdb\",\n",
    "\"SVI2020_WYOMING_tract.gdb\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Initialize an empty list to store GeoDataFrames\n",
    "gdframes = []\n",
    "import os\n",
    "# Loop through all the file names\n",
    "for file in file_names:\n",
    "    # Check if file exists\n",
    "    if os.path.exists(\"data/\" + file):\n",
    "        # Load the file into a GeoDataFrame\n",
    "        gdf = gpd.read_file(\"data/\" + file)\n",
    "        \n",
    "        # Append the GeoDataFrame to the list\n",
    "        gdframes.append(gdf)\n",
    "    else:\n",
    "        print(f'File {file} does not exist.')\n",
    "\n",
    "# Concatenate all GeoDataFrames in the list\n",
    "combined_gdf = pd.concat(gdframes, ignore_index=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18130"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "combined_gdf['FIPS'].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "combined_gdf = combined_gdf.merge(df, right_on=\"GEOID\", left_on=\"FIPS\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "combined_gdf.to_file(\"outputs/d2-events-2d-230929_SVI_shapefile.geojson\", driver='GeoJSON')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
